using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using LinqToWIQL;
using Microsoft.TeamFoundation.Client;

namespace IQToolkit.Data.SqlClient
{
    public class TFSConnection
    {
        private string _projectname;
        
        
        public ConnectionState State;
        public Uri ServerUrl { get; set; }
        
      


        public String ProjectName 
        {
            get { return _projectname; }
            set { _projectname= value; }
        }


        protected TfsConfigurationServer FundationServer { get; set; }


        public TFSConnection(Uri serverurl)
        {
            
        }

        public TFSConnection(Uri serverurl, string userName,string password)
        {

        } 

        public TFSConnection(Uri serverurl, string userName, string password, string projectname)
        {
            NetworkCredential credential;
            if (userName != string.Empty && password != string.Empty)
            {
                credential = new NetworkCredential(userName,password);
            }
            else
            {
                credential = CredentialCache.DefaultNetworkCredentials;
            }

            FundationServer = new TfsConfigurationServer(ServerUrl,credential);
        }

        public void Open()
        {
           if(FundationServer==null)
               throw new MissingFieldException("TFSConnection","FundationServer");

            try
            {
                State = ConnectionState.Connecting;
                FundationServer.Authenticate();
                State = ConnectionState.Open;
            }
            catch (Exception e)
            {
                State = ConnectionState.Broken;
                throw;
            }
        }

        public void Close()
        {
            if (FundationServer == null)
                throw new MissingFieldException("TFSConnection", "FundationServer");

            try
            {
                FundationServer.Dispose();
                State = ConnectionState.Closed;
            }
            catch (Exception e)
            {
                State = ConnectionState.Broken;
                throw;
            }

            State = ConnectionState.Closed;
        }


        public List<TFSProject> GetProjects()
        {
            return  new List<TFSProject>();
        }



        public TFSCommand CreateCommand()
        {
            var c = new TFSCommand(this);
            this.Close();
            return c;
        }



    }
}